package org.winexpress

import grails.converters.JSON
import org.codehaus.groovy.grails.web.json.JSONObject
import org.codehaus.groovy.grails.web.json.JSONArray

class WineRegionController {
    // shared methods
    def sendResponse(statusCode) {
        if (statusCode == 404) {
            JSONObject o = new JSONObject()
            o.put(statusCode, true)
            render o as JSON
        }
        
        response.status = statusCode
        render response
        
        
        // 200-OK (e.g. GET success)
        // 201-Created (e.g. POST success)
        // 403-Forbidden (e.g. LOGIN failure)
        // 400-Bad Request (e.g. PUT,POST,DELETE failure)
        // 404-Not Found (e.g. GET failure)
        // 500-Internal Server Error (other...)
    }
    
    

    // REST operations
    // GET
    def show = {
//        println 'WineRegionController >> Show >> START'
        
        WineRegionManagement wrman = new WineRegionManagement()
        
        if (params.id && WineRegion.exists(params.id)) {    
            def wr = wrman.getWineRegionByID(params.id)
            render wr as JSON
            
//            println 'Show wine type by ID >> ' + wr.id + " " + wr
        }
        else {
            def allWR = wrman.listWineRegions()
            if (allWR.length() > 0) {
                render allWR as JSON
                sendResponse(200)
            }
            else {
                sendResponse(404)
            }   
            
//            println 'Listing all wine regions >> ' + allWR
        }
        
//        println 'WineRegionController >> Show >> END'
    }
    
    // PUT
    def update = {
        println 'WineRegionController >> Update >> START'
        
        JSONObject o = request.JSON
        
        WineRegionManagement wrman = new WineRegionManagement()
        CountryManagement ctyman = new CountryManagement()
        
        def wr = wrman.getWineRegionByID(o.get("id"))
        if (o.has("wineRegionName")) { wr.wineRegionName = o.get("wineRegionName"); println wr.wineRegionName }
        if (o.has("description")) { wr.description = o.get("description"); println wr.description }
        if (o.has("country")) {
            def cty = ctyman.getCountryForDbByCountryName(o.get("country"));
            if (cty == null) {
                println 'WineRegionController >> Save >> Country not found.'
//                sendResponse(404)
            }
            else {
                wr.countryId = cty
            }
        }  
        
        def result = wrman.updateWineRegion(wr)
        if (result == true) {
            println 'WineRegionController >> Update >> ' + result
            sendResponse(200)
        }
        else {
            println 'WineRegionController >> Update >> ' + result
            sendResponse(400)
        }

        println 'WineRegionController >> Update >> END'

    }
    
    // DELETE
    def delete = {
        println 'WineRegionController >> delete >> START'
        
        WineRegionManagement wrman = new WineRegionManagement()
        
        if (params.id && WineRegion.exists(params.id)) {    
            def result = wrman.deleteWineRegion(id)
            
            if (result == true) {
                println 'WineRegionController >> delete >> ' + result
                sendResponse(200)
            }
            else {
                println 'WineRegionController >> delete >> ' + result
                sendResponse(400)
            }
        }
        
        println 'WineRegionController >> delete >> END'
    }
    
    // POST
    def save = {
        println 'WineRegionController >> Save >> START'
        
        JSONObject o = request.JSON
        
        WineRegionManagement wrman = new WineRegionManagement()
        
        if (isUniqueWineRegionName(o.get("wineRegionName")) == true) {
            def result = wrman.addWineRegion(o)
            if (result == true) {
                println 'WineRegionController >> Save >> ' + result
                sendResponse(201)
            }
            else {
                println 'WineRegionController >> Save >> ' + result
                sendResponse(400)
            }
        }
        else {
            sendResponse(406)
        }

        println 'WineRegionController >> Save >> END'
    }
    
    // supporting operations
    def isUniqueWineRegionName(wineRegionName) {
        
        def status = false
        
        WineRegionManagement wrman = new WineRegionManagement()
        WineRegion wr = null
        
        wr = wrman.getWineRegionForDbByWineRegionName(wineRegionName)
        
        if (wr == null) {
            status = true 
        }
        
        return status
    }
}
